package com.one.common.view.pagestate.listpage;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.one.common.R;
import com.one.common.view.pagestate.refreshlayout.IHeaderView;
import com.one.common.view.pagestate.refreshlayout.OnAnimEndListener;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2018/11/21 15:11
 * Description：下拉更多
 **/
public class HeaderView1 extends LinearLayout implements IHeaderView {
    private LottieAnimationView animationView;
    public HeaderView1(Context context) {
        super(context);


    }

    @Override
    public View getView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_header_view, this);
        animationView = findViewById(R.id.lottie_header);
        return view;
    }


    @Override
    public void onPullingDown(float fraction, float maxHeadHeight, float headHeight) {
        if(animationView.isAnimating()) {
            animationView.pauseAnimation();
        }
    }

    @Override
    public void onPullReleasing(float fraction, float maxHeadHeight, float headHeight) {
        if(animationView.isAnimating()) {
            animationView.pauseAnimation();
        }
    }

    @Override
    public void startAnim(float maxHeadHeight, float headHeight) {
        animationView.playAnimation();
    }

    @Override
    public void onFinish(OnAnimEndListener animEndListener) {
        animationView.animate().translationX(0).translationY(0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                reset();
                animEndListener.onAnimEnd();
            }
        }).start();
    }

    @Override
    public void reset() {
        animationView.setProgress(0);
        animationView.cancelAnimation();
    }
}
